﻿using System;
using System.Windows.Forms;

namespace BotonJXC.Forms.Controls
{
    /// <summary>
    /// 分类控件
    /// </summary>
    public partial class Pager : BaseUserControl
    {
        private string _pagerInfo;
        private int _totalCount;
        private int _pageSize = 10;
        /// <summary>
        /// 在页码改变时发生
        /// </summary>
        public event EventHandler PageChanged = null;
        /// <summary>
        /// 页面开始位置
        /// </summary>
        public int PageStart
        {
            get {
                return (PageNo - 1) * PageSize;
            }
        }
        /// <summary>
        /// 获取或设置当前页码
        /// </summary>
        public int PageNo { get; set; }
        /// <summary>
        /// 获取或设置每页显示数量
        /// </summary>
        public int PageSize
        {
            get
            {
                if (_pageSize < 1)
                    _pageSize = 10;
                return _pageSize;
            }
            set { _pageSize = value; }
        }
        /// <summary>
        /// 获取总页数
        /// </summary>
        public int PageCount
        {
            get
            {
                int pageCount = TotalCount % PageSize == 0 ? TotalCount / PageSize : TotalCount / PageSize + 1;
                return pageCount < 1 ? 1 : pageCount;
            }
        }
        /// <summary>
        /// 获取或设置总记录数
        /// </summary>
        public int TotalCount
        {
            get
            {
                return _totalCount;
            }
            set
            {
                _totalCount = value;
                klPagerInfo.Text = string.Format(_pagerInfo, value);
            }
        }
        public Pager()
        {
            InitializeComponent();
            _pagerInfo = klPagerInfo.Text;
            PageSize = 10;
        }

        private void ktbGo_Click(object sender, EventArgs e)
        {
            int page = BotonJXC.Util.Utils.ToInt(kmtbPage.Text.Trim(), 0);
            if (page > 0 && page <= this.PageCount)
                GoPage(page);
        }

        private void GoPage(int pageNo)
        {
            if (this.PageNo != pageNo)
            {
                this.PageNo = pageNo;
                kmtbPage.Text = pageNo.ToString();
                if (this.PageChanged != null)
                    PageChanged(this, EventArgs.Empty);
            }
        }

        private void kbFirst_Click(object sender, EventArgs e)
        {
            GoPage(1);
        }

        private void kbPrevious_Click(object sender, EventArgs e)
        {
            if (this.PageNo > 1)
                GoPage(this.PageNo - 1);
        }

        private void kbNext_Click(object sender, EventArgs e)
        {
            if (this.PageNo < this.PageCount)
                GoPage(this.PageNo + 1);
        }

        private void kbLast_Click(object sender, EventArgs e)
        {
            GoPage(this.PageCount);
        }

        private void Pager_Load(object sender, EventArgs e)
        {
            klPagerInfo.Font = new System.Drawing.Font(System.Drawing.SystemFonts.DefaultFont.FontFamily, 10);
            ResetLayout();
        }

        private void Pager_SizeChanged(object sender, EventArgs e)
        {
            ResetLayout();
        }

        private void ResetLayout()
        {
            klPagerInfo.Location = new System.Drawing.Point(panel1.Location.X - klPagerInfo.Width - 70, klPagerInfo.Location.Y);
        }
    }
}